﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyATM.Common;
using MyATM.DataAccess;
using MyATM.Entity;

namespace MyATM.Bussiness
{
    public class ClientSystem
    {
        private Account account;
        private Card card;
        private AccountDAO accountDao;
        private CardDA cardDa;
        private AtmMachine atmMachine;
        private readonly MainSystem mainSystem;
        private int countCorrectAuthen;

        #region Properties
        public AtmMachine AtmMachine
        {
            get { return atmMachine; }
            set { atmMachine = value; }
        }
        public Account Account
        {
            get { return account; }
            set { account = value; }
        }
        public Card Card
        {
            get { return card; }
            set { card = value; }
        }
        public  AccountDAO AccountDao
        {
            get { return accountDao; }
            set { accountDao = value; }
        }
        public CardDA CardDa
        {
            get { return cardDa; }
            set { cardDa = value; }
        }
        public int CountCorrectAuthen
        {
            get { return countCorrectAuthen; }
            set { countCorrectAuthen = value; }
        }
        #endregion 
        //Constructor
        public ClientSystem()
        {
            card = new Card();
            cardDa = new CardDA();
            accountDao = new AccountDAO();
            mainSystem = new MainSystem();
        }
        /// <summary>
        /// Ham thay doi pin
        /// </summary>
        /// <param name="newPin"></param>
        /// <param name="confirmPin"></param>
        /// <returns></returns>
        public bool ChangePin(string newPin, string confirmPin)
        {
            if (string.IsNullOrEmpty(newPin))
            {
                return false;
            }
            if(string.IsNullOrEmpty(confirmPin))
            {
                return false;
            }
            if (newPin == confirmPin)
            {
                if(newPin.Length < 6)
                {
                    return false;
                }
                cardDa = new CardDA();
                cardDa.UpdatePinCard(Card.CardId, newPin);
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Ham xem so du tai khoan
        /// </summary>
        /// <returns></returns>
        public double ViewBalance()
        {
            double balance = accountDao.ViewBalance(Card.CardId);
            return balance;
        }
        /// <summary>
        /// Ham in so du
        /// </summary>
        /// <returns></returns>
        public string PrintBlance()
        {
            string resultString = "";
            resultString = "Ma tai khoan: " + Account.AccountId + "\n";
            resultString += "So du tai khoan: " + Account.Balance + "\n";
            return resultString;
        }
        /// <summary>
        /// Ham thuc hien rut tien.
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public string WithdrawMoney(string cardId, double amount)
        {
            Card.CardId = cardId;
            string resultString = "";
            double balance = AccountDao.ViewBalance(cardId);
            double dailyAmount = CardDa.GetAmountInDay(cardId);
            double overDraftLimit = AccountDao.GetOverdraftLimit(cardId);

            if (amount < balance)
            {
                if (amount < overDraftLimit - dailyAmount)
                {
                    Transaction transaction = new Transaction(DateTime.Now, amount, 1);
                    TransactionDA transactionDa = new TransactionDA();
                    transactionDa.TransactionLogging(transaction, cardId);
                    resultString = CardReader.EjectCard();
                    resultString += "Transaction is success.";
                }
                else
                {
                    resultString = "The amount of transaction per day is: " + overDraftLimit.ToString();
                }
            }
            else
            {
                resultString = "The balance of your account is not enought to draw.";

            }
            return resultString;
        }
        /// <summary>
        /// Xac thuc card nhap vao
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="cardPin"></param>
        /// <returns></returns>
        public bool AuthenticateUser(string cardId, string cardPin)
        {
            if (!string.IsNullOrEmpty(cardId) && !string.IsNullOrEmpty(cardPin) 
                && cardPin.Length == 6 && cardId.Length == 4)
            {
                bool result = mainSystem.AuthenticateCard(cardId, cardPin);
                if (result == false)
                {
                    countCorrectAuthen++;
                    return false;
                }
                return result;
            }
            else
            {
                return false;
            }
            
        }
        /// <summary>
        /// Xac thuc card
        /// </summary>
        /// <param name="cardId"></param>
        /// <returns></returns>
        public bool ValidateCard(string cardId)
        {
            if (!string.IsNullOrEmpty(cardId))
            {
                bool result = CardDa.ValidateCard(cardId);
                return result;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// In giao dich
        /// </summary>
        /// <param name="amount"></param>
        /// <returns></returns>
        public string PrintTransaction(double amount)
        {
            string resultString = "";
            resultString = "Account ID: " + Account.AccountId;
            resultString += "Ngay rut tien: " + DateTime.Now.ToShortDateString();
            resultString += "So tien da rut: " + amount.ToString();
            return resultString;
        }
        /// <summary>
        /// Khoa tam thoi the card
        /// </summary>
        /// <param name="cardId"></param>
        /// <returns></returns>
        public bool BlockCard(string cardId)
        {
            bool result = mainSystem.BlockCard(cardId);
            return result;
        }
    }
}
